package com.xuef.ibatis.session.defaults;

import com.xuef.ibatis.executor.Executor;
import com.xuef.ibatis.mapping.Environment;
import com.xuef.ibatis.session.Configuration;
import com.xuef.ibatis.session.SqlSession;
import com.xuef.ibatis.session.SqlSessionFactory;

import java.sql.Connection;

/**
 * Created by xuefengDong: moveb
 * On 2019/4/26- 18:57
 */
public class DefaultSqlSessionFactory implements SqlSessionFactory {
    private final Configuration configuration;

    public DefaultSqlSessionFactory(Configuration configuration) {
        this.configuration = configuration;
    }

    public SqlSession openSession(Connection connection) {
        return openSessionFromConnection(connection);
    }

    private SqlSession openSessionFromConnection(Connection connection) {
        try {
            final Environment environment = configuration.getEnvironment();
            final Executor executor = configuration.newExecutor(connection);

            return new DefaultSqlSession(configuration, executor);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
